package com.joy.common.handler;

import com.joy.common.exception.JsonException;
import com.joy.common.exception.PageException;
import com.joy.common.enums.Status;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: 统一异常处理
 * @Author: yjzhao
 * @Create: 2019-09-19  09:34
 **/
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * @Description 权限不足
     * @param req
     * @param e
     * @return
     * @Author yjzhao
     * @Date 2019/9/19 10:14
     **/
    @ExceptionHandler(value = {org.apache.shiro.authz.UnauthenticatedException.class,
            org.apache.shiro.authz.UnauthorizedException.class})
    public ModelAndView unauthenticatedErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("pages/error/403");
        return mav;
    }

    /**
     * @Description 没有此页面
     * @param req
     * @param e
     * @return
     * @Author yjzhao
     * @Date 2019/9/19 10:14
     **/
    @ExceptionHandler(value = NoHandlerFoundException.class)
    public ModelAndView noPageErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject(e);
        mav.setViewName("pages/error/404");
        return mav;
    }

    /**
     * @Description json error
     * @param req
     * @param e
     * @return
     * @Author yjzhao
     * @Date 2019/9/19 10:14
     **/
    @ExceptionHandler(value = JsonException.class)
    @ResponseBody
    public JsonException jsonErrorHandler(HttpServletRequest req, JsonException e) throws Exception {
        return e;
    }

    /**
     * @Description json error
     * @param req
     * @param e
     * @return
     * @Author yjzhao
     * @Date 2019/9/19 10:14
     **/
    @ExceptionHandler(value = PageException.class)
    @ResponseBody
    public ModelAndView pageErrorHandler(HttpServletRequest req, PageException e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("error", e);
        mav.setViewName("pages/error/500");
        return mav;
    }


    /**
     * @Description controller 返回异常
     * @param req
     * @param e
     * @return
     * @Author yjzhao
     * @Date 2019/9/19 10:14
     **/
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ModelAndView commonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        e.printStackTrace();
        PageException pageException = new PageException(Status.UNKNOWN_ERROR);
        ModelAndView mav = new ModelAndView();
        mav.addObject("error", pageException);
        mav.setViewName("pages/error/500");
        return mav;
    }
}
